/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package slsBeans;

import EL.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class TableBLO implements TableBLORemote {
    
    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<RestaurantTables> getAll()
    {
        List<RestaurantTables> tables=null;
        try
        {
            String hql = "FROM RestaurantTables";
            Query query = this.em.createQuery(hql);
            tables = query.getResultList();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return tables;
    }

    public RestaurantTables getByID(int id)
    {
        RestaurantTables table = this.em.find(RestaurantTables.class, id);
        return table;
    }

    public boolean add(RestaurantTables table, TableTypes tableType, DiningRooms room)
    {
        List<RestaurantTables> tables = new ArrayList<RestaurantTables>();
        tables.add(table);

        table.setDiningRooms(room);
        table.setTableTypes(tableType);
        tableType.setTablesList(tables);
        room.setTablesList(tables);

        try
        {
            this.em.merge(table);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        
    }

    public boolean edit(RestaurantTables table)
    {
        try
        {
            this.em.merge(table);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public boolean delete(int id)
    {
        RestaurantTables table = getByID(id);
        if(table != null)
        {
            try
            {
                this.em.remove(table);
                return true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

}
